<template>
  <div>
    <!-- 弹出层的头部区域 -->
    <van-nav-bar title="频道管理">
      <template #right>
        <van-icon @click="$emit('close')" name="cross" size="0.37333334rem" color="white" />
      </template>
    </van-nav-bar>
    <!-- 我的频道 -->
    <div class="my-channel-box">
      <div class="channel-title">
        <span
          >我的频道
          <span class="small-title"> 点击{{ isShow ? '删除' : '进入'}}频道 </span>
        </span>
        <span @click="editBtn">{{ isShow ? '完成' : '编辑' }}</span>
      </div>
      <!-- 我的频道列表 -->
      <van-row type="flex">
        <van-col span="6" v-for="item in list" :key="item.id" @click="delBtn(item)">
          <div class="channel-item van-hairline--surround">
            {{ item.name }}
            <!-- 删除的徽标 -->
            <van-badge color="transparent" class="cross-badge" v-show="isShow && item.id !== 0">
              <template #content>
                <van-icon name="cross" class="badge-icon" color="#cfcfcf" size="0.32rem" />
              </template>
            </van-badge>
          </div>
        </van-col>
      </van-row>
    </div>

    <!-- 更多频道 -->
    <div class="more-channel-box">
      <div class="channel-title">
        <span>点击添加更多频道：</span>
      </div>
      <!-- 更多频道列表 -->
      <van-row type="flex">
        <van-col span="6" v-for="obj in getNewList" :key="obj.id" @click="addBtn(obj)">
          <div class="channel-item van-hairline--surround">{{ obj.name }}</div>
        </van-col>
      </van-row>
    </div>
  </div>
</template>

<script>
import { getChannelsList, updateListAPI } from '@/api'
export default {
  name: 'ChannelEdit',
  props: {
    // 已选频道数组
    list: {
      type: Array
    }
  },
  data () {
    return {
      allLists: [], // 所有频道数组
      isShow: false
    }
  },
  mounted () {
    this.getAllLists()
  },
  methods: {
    async getAllLists () {
      const res = await getChannelsList()
      // console.log(res)
      this.allLists = res.data.data.channels
    },
    // 编辑按钮
    editBtn () {
      if (this.isShow) {
        this.updateListFn()
      }
      this.isShow = !this.isShow
    },
    // 新增按钮
    addBtn (addObj) {
      if (this.isShow) {
        // console.log(addObj)
        const arr = [...this.list]
        arr.push(addObj)
        this.$emit('update:list', arr)

        // this.$nextTick(() => {
        //   this.updateListFn()
        // })
      }
    },
    // 更新我的频道
    async updateListFn () {
      const newlist = this.list.map((item, index) => {
        return {
          id: item.id,
          seq: index
        }
      })
      console.log(newlist)
      await updateListAPI({ channels: newlist })
    },
    // 删除我的频道
    delBtn (item) {
      if (this.isShow && item.id !== 0) {
        const arr = [...this.list]
        const index = arr.findIndex(obj => obj.id === item.id)
        arr.splice(index, 1)
        this.$emit('update:list', arr)
      } else { // 切换我的频道
        this.$emit('changeEV', item)
        // 关闭弹出层
        this.$emit('close')
      }
    }
  },
  // 计算属性
  computed: {
    getNewList () {
      const newArr = [] // 未选中的频道数组
      this.allLists.forEach(item => {
        const unList = this.list.find(obj => {
          return item.id === obj.id
        })

        if (unList === undefined) {
          newArr.push(item)
        }
      })
      return newArr
    }
  }
}
</script>

<style scoped lang="less">
.van-popup,
.popup-container {
  background-color: transparent;
  height: 100%;
  width: 100%;
}

.popup-container {
  display: flex;
  flex-direction: column;
}

.pop-header {
  height: 90px;
  background-color: #007bff;
  color: white;
  text-align: center;
  font-size: 14px;
  position: relative;
  .title {
    width: 100%;
    position: absolute;
    bottom: 15px;
  }
}

.pop-body {
  flex: 1;
  overflow: scroll;
  padding: 8px;
  background-color: white;
}

.my-channel-box,
.more-channel-box {
  .channel-title {
    display: flex;
    justify-content: space-between;
    font-size: 14px;
    line-height: 28px;
    padding: 0 6px;
  }
}

.channel-item {
  font-size: 12px;
  text-align: center;
  line-height: 36px;
  background-color: #fafafa;
  margin: 5px;
}

/*删除的微标 */
.cross-badge {
  position: absolute;
  right: -3px;
  top: 0;
  border: none;
}

/*提示文字 */
.small-title {
  font-size: 10px;
  color: gray;
}
</style>
